/**
 * Addition to Autocompleter for Contao
 *
 * http://digitarald.de/project/autocompleter/
 *
 * @version		1.1.2
 *
 * @license		MIT-style license
 * @author		Harald Kirschner <mail [at] digitarald.de>
 * @copyright	Author
 */

Autocompleter.Request.JSON_CTO = new Class({

	Extends: Autocompleter.Request,

	initialize: function(el, url, options) {
		this.parent(el, options);
		this.request = new Request.JSON($merge({
			'url': url,
			'link': 'cancel'
		}, this.options.ajaxOptions)).addEvent('onComplete', this.queryResponse.bind(this));
	},

	queryResponse: function(response) {
		this.parent();
		this.options.postData.REQUEST_TOKEN = response.token;
		// Update all forms
		$$('input[type="hidden"]').each(function(el) {
			if (el.name == 'REQUEST_TOKEN') {
				el.value = response.token;
			}
		});
		this.update(response.content);
	}

});
